<template>
  <div id="app">
    <h1>用户列表</h1>
    <div class="head">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input
              v-model="userInfo.name"
              placeholder="请输入姓名"
          ></el-input>
        </el-col>
        <el-col :span="6">
          <el-input
              v-model="userInfo.gender"
              placeholder="请输入性别"
          ></el-input>
        </el-col>
        <el-col :span="6">
          <el-input
              v-model="userInfo.PhoneNum"
              placeholder="请输入电话"
          ></el-input>
        </el-col>
        <el-date-picker
            v-model="userInfo.Birthday"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM月 dd 日"
            value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-row>
      <el-button type="primary" class="add-btn" @click="addUser"
      >添加用户
      </el-button
      >
    </div>
    <div class="body">
      <template>
        <el-table :data="tableData" v-model="tableData" style="width: 100%">
          <el-table-column label="序号" width="180">
            <template slot-scope="scope"> {{ scope.$index + 1 }}</template>
          </el-table-column>
          <el-table-column prop="name" label="姓名" width="180">
          </el-table-column>
          <el-table-column prop="gender" label="性别"></el-table-column>
          <el-table-column prop="PhoneNum" label="电话号码">
          </el-table-column>
          <el-table-column prop="Birthday" label="生日"></el-table-column>
          <el-table-column prop="Birthday" label="操作">
            <template slot-scope="scope">
              <el-button
                  type="primary"
                  icon="el-icon-edit"
                  @click="editUser(scope.row, scope.$index)"
                  circle
              ></el-button>
              <el-button
                  type="danger"
                  icon="el-icon-delete"
                  @click="delUser(scope.$index)"
                  circle
              >
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </div>
    <el-dialog
        title="编辑用户信息"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
    >
      <div>
        <el-form ref="form" :model="editObj" label-width="80px">
          <el-form-item label="姓名">
            <el-input v-model="editObj.name"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-input v-model="editObj.gender"></el-input>
          </el-form-item>
          <el-form-item label="电话">
            <el-input v-model="editObj.PhoneNum"></el-input>
          </el-form-item>
          <el-form-item label="生日">
            <el-date-picker
                v-model="editObj.Birthday"
                type="date"
                placeholder="选择日期"
                format="yyyy 年 MM月 dd 日"
                value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirm"
          >确 定</el-button
          >
        </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data: function () {
    return {
      // 用户列表
      // datasAll:{},


      keyWord:'',
      userInfo: { //用户信息
        name: '',
        gender: '',
        PhoneNum: '',
        Birthday: '',

      },
      tableData: [{
        name: '张三',
        gender: '男',
        PhoneNum: '18292839282',
        Birthday: '2010-10-2'
      },
      ],
      dialogVisible: false,//弹窗显示
      editObj: {
        name: '',
        gender: '',
        PhoneNum: '',
        Birthday: '',
      },
      userIndex: 0,
    }
  },

  created() {
    //判断如果内存中没有数值则赋值原tableData，如果有数值则赋值新数值
    let NewData = JSON.parse(localStorage.getItem('userdata'))
    // eslint-disable-next-line no-empty
    if (NewData == null){
    }else{
      this.tableData = JSON.parse(localStorage.getItem('userdata'))
    }



  },
  computed:{

    // eslint-disable-next-line vue/no-dupe-keys
    tableData(){
      return this.tableData.filter((data)=>{
        return this.tableData.name.indexOf(this.keyWord) !== -1
      })
    }
  },
  methods: {
    //添加用户信息

    addUser() {
      //判断是否输入了字符

      if (!this.userInfo.name) {
        this.$message({
          message: '请输入姓名',
          type: 'warning'
        });
        return
      }
      if (!this.userInfo.gender) {
        this.$message({
          message: '请输入性别',
          type: 'warning'
        });
        return
      }
      if (!this.userInfo.PhoneNum) {
        this.$message({
          message: '请输入电话',
          type: 'warning'
        });
        return
      }
      if (!/^1[3456789]\d{9}$/.test(this.userInfo.PhoneNum)) {
        this.$message({
          message: '请输入正确的电话',
          type: 'warning'
        });
        return
      }
      if (!this.userInfo.Birthday) {
        this.$message({
          message: '请输入生日',
          type: 'warning'
        });
        return
      }

      this.tableData.push(this.userInfo)
      //使用本地缓存储存数据
      localStorage.setItem('userdata', JSON.stringify(this.tableData))

      this.userInfo = {
        name: '',
        gender: '',
        PhoneNum: '',
        Birthday: '',
      };

    },
    //删除一组数据
    delUser(index) {
      // console.log(index)
      this.$confirm('确认删除？')
          // eslint-disable-next-line no-unused-vars
          .then(_ => {
            this.tableData.splice(index, 1)
          })
          // eslint-disable-next-line no-unused-vars
          .catch(_ => {
          });

    },
    //编辑数据
    editUser(item, index) {
      this.userIndex = index;
      this.editObj = {
        name: item.name,
        gender: item.gender,
        PhoneNum: item.PhoneNum,
        Birthday: item.Birthday,
      }
      this.dialogVisible = true
    },
    handleClose() {
      this.dialogVisible = false
    },
    confirm() {
      //console.log(this === vm)
      // this.tableData[this.userIndex] = this.editObj
      this.$set(this.tableData, this.userIndex, this.editObj)
      this.dialogVisible = false
    }
  }

}


</script>

<style scoped>
#app {
  width: 1024px;
  margin: 0 auto;

}

.add-btn {
  margin-top: 20px;
  width: 100%;
}

.body {
  margin-top: 20px;
}
</style>
